package work.MrSun.xyq.util.isCommentReply;

import work.MrSun.xyq.model.XyqAttend;
import work.MrSun.xyq.model.XyqCommentReply;
import work.MrSun.xyq.model.XyqNewMessage;
import work.MrSun.xyq.model.XyqUser;
import work.MrSun.xyq.service.XyqAttendService;
import work.MrSun.xyq.service.XyqCommentReplayService;
import work.MrSun.xyq.service.XyqNewMessageService;
import work.MrSun.xyq.service.XyqUserService;

/**
 * @Author MrSun
 * @Date 2023/4/30 15:54
 */

/**
 * 评论回复工具类
 */
public class IsCommentReply {
    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public IsCommentReply isTrue(Integer messageId, XyqCommentReply xyqCommentReply, XyqUserService xyqUserService, XyqCommentReplayService xyqCommentReplayService, XyqNewMessageService xyqNewMessageService, XyqAttendService xyqAttendService) {
        IsCommentReply isCommentReply = new IsCommentReply();
        isCommentReply.setCode(500);
        XyqUser user = xyqUserService.getById(xyqCommentReply.getReplayUserId());
        if (user == null) {
            isCommentReply.setCode(400);
            return isCommentReply;
        }

        if(user.getUserAllow()!=1){
            isCommentReply.setCode(301);
            return isCommentReply;
        }

        xyqCommentReplayService.add(xyqCommentReply);

        if (xyqCommentReply.getReceiveUserId() == xyqCommentReply.getReplayUserId()) {
            isCommentReply.setCode(200);
            return isCommentReply;
        }

        if(xyqCommentReply.getReplayUserId()!=xyqCommentReply.getReceiveUserId()) {
            XyqNewMessage mangoNewMessage = new XyqNewMessage();
            mangoNewMessage.setUserId(xyqCommentReply.getReceiveUserId());
            mangoNewMessage.setNewMessageType(2);
            mangoNewMessage.setMessageId(messageId);
            mangoNewMessage.setNewMessageDetail(xyqCommentReply.getReplyDetail());
            xyqNewMessageService.add(mangoNewMessage);
        }

        XyqAttend mangoAttend = new XyqAttend();
        mangoAttend.setMessageId(messageId);
        mangoAttend.setUserId(xyqCommentReply.getReplayUserId());

        if (xyqAttendService.findCount(mangoAttend) != 0) {
            isCommentReply.setCode(200);
            return isCommentReply;
        }

        xyqAttendService.add(mangoAttend);

        isCommentReply.setCode(200);
        return isCommentReply;
    }
}